package com.netease.framework.db;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import com.netease.framework.db.DBSupport.CollectorTable;
import com.netease.framework.db.DBSupport.PreferenceTable;

/**
 * provider
 * @author Panjf
 * @date   2011-10-9
 */
public class FrameProvider extends ContentProvider {
	private static final int MATCH_PREFERENCE = 1;
	private static final int MATCH_COLLECTOR = 2;
	
	private DBSupport mOpenHelper;
	private static final UriMatcher sUriMatcher;

	static{
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(DBSupport.AUTHORITY, PreferenceTable.TABLE_NAME, MATCH_PREFERENCE);
		sUriMatcher.addURI(DBSupport.AUTHORITY, CollectorTable.TABLE_NAME, MATCH_COLLECTOR);
	}
	
	@Override
	public boolean onCreate() {
		try 
        {
            mOpenHelper = DBSupport.getInstance(getContext());
            mOpenHelper.getWritableDatabase();
        }
        catch (SQLiteException e)
        {
            e.printStackTrace();
            mOpenHelper.close();
            mOpenHelper = null;
        }
        
        return true;
	}
	
	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		//返回啥??
		return null;
	}
	
	
	@Override
	public int delete(Uri url, String where, String[] whereArgs) {
		int count = 0;
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int match = sUriMatcher.match(url);

		db.beginTransaction();
		try {
			switch (match) {
				case MATCH_PREFERENCE:
					count = db.delete(PreferenceTable.TABLE_NAME, where, whereArgs);
					break;
				case MATCH_COLLECTOR: {
					count = db.delete(CollectorTable.TABLE_NAME, where, whereArgs);
					break;
				}
				default: {
					throw new UnsupportedOperationException(
							"Cannot delete that URL: " + url);
				}
			}

			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}

		if (count > 0) {
			getContext().getContentResolver().notifyChange(url, null);
		}

		return count;
	}
	

	@Override
	public Uri insert(Uri url, ContentValues initialValues) {
		Uri result = null;

		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int match = sUriMatcher.match(url);

		db.beginTransaction();
	    try{
			switch (match) {
	    		case MATCH_PREFERENCE: {
	    			long rowID = db.insert(PreferenceTable.TABLE_NAME, null, initialValues);
	    			if (rowID > 0) {
	    				result = ContentUris.withAppendedId(PreferenceTable.Preference_URI, rowID);
	    			}
	    		}
	    		break;
	    		
	    		case MATCH_COLLECTOR:{
	    			long rowID = db.insert(CollectorTable.TABLE_NAME, null, initialValues);
	    			if(rowID > 0){
	    				result = ContentUris.withAppendedId(CollectorTable.Collector_URI, rowID);
	    			}
	    		}
	    		break;
	    		
	    		default:{
	    			throw new UnsupportedOperationException(
							"Cannot insert that URL: " + url);
	    		}
			}
			
			db.setTransactionSuccessful();
        }finally{
        	db.endTransaction();
        }
        
		getContext().getContentResolver().notifyChange(url, null);

		return result;
	}



	@Override
	public Cursor query(Uri url, String[] projectionIn, String selection,
	                    String[] selectionArgs, String sort) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		
		int match = sUriMatcher.match(url);
		switch (match) {
		case MATCH_PREFERENCE:
			qb.setTables(PreferenceTable.TABLE_NAME);
			break;

		case MATCH_COLLECTOR:
			qb.setTables(CollectorTable.TABLE_NAME);
			break;
			
		default:
			throw new UnsupportedOperationException(
					"Cannot query that URL: " + url);
		}

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor ret = qb.query(db, projectionIn, selection, selectionArgs, null, null, sort);
        ret.setNotificationUri(getContext().getContentResolver(), url);
        return ret;
	}

	@Override
	public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
		int count = 0;

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int match = sUriMatcher.match(url);
        
        db.beginTransaction();
        try{
        
	        switch (match)
	        {
	            case MATCH_PREFERENCE:
	                count = db.update(PreferenceTable.TABLE_NAME, values, where, whereArgs);
	                break;

	            case MATCH_COLLECTOR:
	            	count = db.update(CollectorTable.TABLE_NAME, values, where, whereArgs);
	            	break;

	            default: {
	                throw new UnsupportedOperationException("Cannot update that URL: " + url);
	            }
	        }
	        db.setTransactionSuccessful();
        }finally{
        	db.endTransaction();
        }
        
        if (count > 0) {
            getContext().getContentResolver().notifyChange(url, null);
        }

        return count;
	}

}
